package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.ht.medicaltreatment.bean.*
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class StockViewModel : BaseViewModel() {

    fun requestData(id:String,onResult:(StockBean)->Unit,onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.requestStockData(id)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun requestDataFromLocation(id:String, onResult:(StockLocationBean)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.requestDataFromLocation(id)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun submitLocation(json: LocationSubmitJson, onResult:(StockSubmitBean?)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.submitLocation(json)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun submitOrder(json: OrderSubmitJson, onResult:(StockSubmitBean)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.submitOrder(json)
            when(bean){
                is ApiResult.Success -> onResult(bean.data.data!!)
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun searchName(uid:MutableList<String>, onResult:(MutableList<SearchUIDBean>)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.searchName(uid)
            when(bean){
                is ApiResult.Success -> {
                    if(bean.data.data != null && bean.data.data!!.size > 0){
                        onResult(bean.data.data!!)
                    }
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}
